package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.GatherListResponse
import com.gexiaobao.pigeon.app.model.bean.PassOrgResponse
import com.gexiaobao.pigeon.app.model.bean.RaceDetailInfoResponse
import com.gexiaobao.pigeon.app.model.bean.SignDeviceListResponse
import com.gexiaobao.pigeon.app.model.bean.TrainingUnDoneListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.NextPassParams
import com.gexiaobao.pigeon.app.model.param.StartFlyParams
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

/**
 * @author:hxw
 * @date  :2023/1/31
 * @desc  :
 */
class ReleaseMissionViewModel : BaseViewModel() {

    private var pageSize = 20
    private var pageNo = 1

    //集鸽明细
    var etSearchContent = StringObservableField()
    var deviceName = StringObservableField("全部")

    //放飞任务
    var startFly = StringObservableField()

    /** 退出登录 */
    val logOutResult: MutableLiveData<UiState> = MutableLiveData()

    /** 开始比赛-结束比赛 (开始放飞)*/
    val startRacingResult: MutableLiveData<UiState> = MutableLiveData()

    /** 获取归巢经纬度 */
    val homingLResult: MutableLiveData<PassOrgResponse> = MutableLiveData()

    /** 放飞员-结束报名 */
    val nextResult: MutableLiveData<UiState> = MutableLiveData()

    /** 获取放飞任务列表 */
    var trainingResult: MutableLiveData<ListDataUiState<TrainingUnDoneListResponse.InfoList>> = MutableLiveData()
//    var trainingResult: MutableLiveData<TrainingUnDoneListResponse> = MutableLiveData()

    /** 集鸽列表信息 */
    val trainingSignUpDetailListResult: MutableLiveData<ListDataUiState<GatherListResponse.DataList>> = MutableLiveData()

    /**  设备列表  */
    var deviceList: MutableLiveData<SignDeviceListResponse> = MutableLiveData()

    /**  比赛详细信息  */
    var detailInfoResult: MutableLiveData<RaceDetailInfoResponse> = MutableLiveData()

    /** 退出登录 */
    fun loginOut() {
        request({ apiService.loginOut() }, {
            val deleteState = UiState(isSuccess = true)
            logOutResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            logOutResult.value = deleteState
        })
    }


    /** 放飞任务 */
    fun getFlyPassList(orgId: String) {
        request({ apiService.getFlyPassList(orgId) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = true,
                isEmpty = it.isEmpty(),
                hasMore = true,
                total = it.total,
                listData = it.list
            )
            trainingResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                errMessage = it.msg,
                isRefresh = false,
                listData = arrayListOf<TrainingUnDoneListResponse.InfoList>()
            )
            trainingResult.value = listDataUiState
        })
    }

    fun getDeviceList(orgId: String) {
        request({ apiService.getGatherDeviceList("1", "99999", orgId) }, {
            deviceList.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    fun getGatherList(isRefresh: Boolean, passId: String, qStr: String, imsi: String, ringId: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getGatherList(passId, qStr, imsi, ringId, pageSize, pageNo) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                total = it.total,
                listData = it.data
            )
            trainingSignUpDetailListResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<GatherListResponse.DataList>()
                )
            trainingSignUpDetailListResult.value = listDataUiState
        })
    }

    fun getPassDetailApp(id: String) {
        request({ apiService.getPassDetailApp(id) }, {
            detailInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 放飞员-开始比赛(开始放飞) */
    fun startRacing(body: StartFlyParams) {
        request({ apiService.startRacing(body) }, {
            val state = UiState(isSuccess = true)
            startRacingResult.value = state
        }, {
            val state = UiState(isSuccess = false, errorMsg = it.msg)
            startRacingResult.value = state
        })
    }

    /** 获取关卡经纬度 */
    fun getLocation(id: String) {
        request({ apiService.getLocation(id) }, {
            homingLResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 放飞员-结束报名 */
    fun getNext(body: NextPassParams) {
        request({ apiService.getNext(body) }, {
            val next = UiState(isSuccess = true)
            nextResult.value = next
        }, {
            val next = UiState(isSuccess = false, errorMsg = it.msg)
            nextResult.value = next
        })
    }

}